package com.lzy.business.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.base.utils.UserUtil;
import com.lzy.business.facade.ProductSaleLevelFacade;
import com.lzy.business.request.ProductSaleLevelRequest;
import com.lzy.business.response.ProductSaleLevelResponse;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.apis.ProductSaleLevelServiceApi;
import com.lzy.product.dtos.ProductSaleLevelDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by WIN7 on 2017/12/12.
 */
@Controller
@RequestMapping(value = "/admin/product/level")
public class ProductSaleLevelController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductSaleLevelController.class);
    @Autowired
    ProductSaleLevelFacade productSaleLevelFacade;

    @Autowired
    ProductSaleLevelServiceApi productSaleLevelServiceApi;

    /**
     * 开通下级列表页面
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String list() {
        return "product/open_list";
    }


    /**
     * 查询列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public PageBean json(ProductSaleLevelRequest request) {
        Ensure.that(request).isNotNull("01000003");
        Pager pager = new Pager();
        pager.setPageSize(request.getPageSize());
        pager.setPageNum(request.getPageNum());
        PageInfo<ProductSaleLevelDto> pageInfo = productSaleLevelFacade.getList(pager, DozerUtils.map(request, ProductSaleLevelDto.class));
        if (null != pageInfo) {
            List<ProductSaleLevelDto> list = pageInfo.getList();
            return new PageBean<ProductSaleLevelResponse>(pageInfo.getTotal(), DozerUtils.maps(list, ProductSaleLevelResponse.class));
        } else {
            return null;
        }
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "/add")
    public String add(Model model) {
        ProductSaleLevelDto productSaleLevelDto = productSaleLevelFacade.toAdd();
        if(productSaleLevelDto != null){
            if(productSaleLevelDto.getClassifyList() != null){
                model.addAttribute("classifyList",productSaleLevelDto.getClassifyList());
            }
            if(productSaleLevelDto.getConditionList() != null){
                model.addAttribute("conditionList",productSaleLevelDto.getConditionList());
            }
        }
        return "product/open_add";
    }

    /**
     * 新增条件
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(ProductSaleLevelRequest request) {
        Ensure.that(request).isNotNull("01000003");
        request.setCreateUser(UserUtil.getCurrentUser().getId());
        Boolean b = productSaleLevelFacade.insert(DozerUtils.map(request, ProductSaleLevelDto.class));
        Ensure.that(b).isTrue("06000003");
        return new Result();
    }

    /**
     * 编辑页面
     */
    @RequestMapping(value = "/edit")
    public String edit(@RequestParam("id") Long id, Model model) {
        Ensure.that(id).isNotNull("01000003");
        ProductSaleLevelDto productSaleLevelDto = productSaleLevelFacade.toEdit(id);
        if(productSaleLevelDto != null){
            model.addAttribute("dto",productSaleLevelDto);
            if(productSaleLevelDto.getClassifyList() != null){
                model.addAttribute("classifyList",productSaleLevelDto.getClassifyList());
            }
            if(productSaleLevelDto.getConditionList() != null){
                model.addAttribute("conditionList",productSaleLevelDto.getConditionList());
            }
            if(productSaleLevelDto.getNumberArr() != null){
                model.addAttribute("numberArr",productSaleLevelDto.getNumberArr());
            }
        }
        return "product/open_add";
    }

    /**
     * 更新条件
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(ProductSaleLevelRequest request) {
        Ensure.that(request).isNotNull("01000003");
        request.setUpdateUser(UserUtil.getCurrentUser().getId());
        Boolean b = productSaleLevelFacade.update(DozerUtils.map(request, ProductSaleLevelDto.class));
        Ensure.that(b).isTrue("06000003");
        return new Result();
    }

    /**
     * 单个移除
     */
    @RequestMapping(value = "/remove", method = RequestMethod.GET)
    @ResponseBody
    public Result remove(@RequestParam("id") Long id) {
        Ensure.that(id).isNotNull("01000003");
        Boolean b = productSaleLevelServiceApi.remove(id);
        Ensure.that(b).isTrue("06000001");
        return new Result();
    }

    /**
     * 批量移除
     */
    @RequestMapping(value = "/removeList", method = RequestMethod.POST)
    @ResponseBody
    public Result removeList(@RequestParam("ids") Long[] idList) {
        Ensure.that(idList).isNotNull("01000003");
        Boolean b = productSaleLevelFacade.removeList(idList);
        Ensure.that(b).isTrue("06000002");
        return new Result();
    }


}
